using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class HitsDisplay : MonoBehaviour
{
	[Serializable]
	public class ColorDesc
	{
		public float fillMax;

		public Color color;
	}

	public UILabel scoreLabel;

	public UILabel ballsLabel;

	public UISprite fillSprite;

	public UILabel titleLabel;

	public UILabel countingLabel;

	public float fillChangeSpeed = 5f;

	public float scoreScaleFrom = 1.4f;

	public float scoreScaleDuration = 0.2f;

	protected float desiredFillAmmount;

	public bool moveTotalBallsDown;

	public List<ColorDesc> colors = new List<ColorDesc>();

	public List<UISprite> stars = new List<UISprite>();

	public GameObject starContainer;

	public UIWidget labelContainer;

	public UISprite star;

	private void Awake()
	{
		UITools.SetActive(labelContainer, false);
	}

	public void SetTitle(string title)
	{
		titleLabel.text = title;
	}

	public void Show(int score, int balls, int totalBalls)
	{
		base.gameObject.SetActive(true);
		SetStarProgression(0f);
		scoreLabel.text = score.ToString();
		UpdateBalls(balls, totalBalls);
	}

	public void ShowCountingLabel(int count)
	{
		countingLabel.cachedGameObject.SetActive(true);
		countingLabel.text = count.ToString();
	}

	private void SetStarProgression(float progression)
	{
		int num = 0;
		foreach (UISprite star in stars)
		{
			star.fillAmount = Mathf.Lerp(0f, 1f, progression - (float)num);
			num++;
		}
	}

	private void SetColorForFill()
	{
		ColorDesc colorDesc = null;
		foreach (ColorDesc color in colors)
		{
			colorDesc = color;
			if (color.fillMax > fillSprite.fillAmount)
			{
				break;
			}
		}
		if (colorDesc != null)
		{
			fillSprite.color = colorDesc.color;
		}
	}

	public void UpdateCountingLabel(int count)
	{
		countingLabel.text = count.ToString();
		StartCoroutine(DoAnimateScoreUpdate(countingLabel));
	}

	public void UpdateBalls(int balls, int totalBalls)
	{
		if (moveTotalBallsDown)
		{
			desiredFillAmmount = Mathf.InverseLerp(0f, totalBalls, balls);
			ballsLabel.text = balls.ToString();
		}
		else
		{
			desiredFillAmmount = Mathf.InverseLerp(0f, totalBalls, balls);
			ballsLabel.text = balls + "/" + totalBalls;
		}
	}

	public void UpdateScore(int score)
	{
		scoreLabel.text = score.ToString();
		if (base.gameObject.activeInHierarchy)
		{
			StartCoroutine(DoAnimateScoreUpdate(scoreLabel));
		}
	}

	public void UpdateScore(int score, Vector3 pos, bool showStar, float starProgression)
	{
		scoreLabel.text = score.ToString();
		UITools.ChangeText(countingLabel, scoreLabel.text);
		if (base.gameObject.activeInHierarchy && labelContainer != null)
		{
			StopAllCoroutines();
			StartCoroutine(DoAnimateScoreUpdate(scoreLabel));
			UIWidget goToWidget = scoreLabel;
			if (showStar)
			{
				int num = Mathf.Max(0, Mathf.FloorToInt(starProgression) - 1);
				if (num < stars.Count)
				{
					goToWidget = stars[num];
				}
			}
			StartCoroutine(DoAnimateScoreUpdateOnPos(pos, showStar, goToWidget));
		}
		SetStarProgression(starProgression);
	}

	private IEnumerator DoAnimateScoreUpdateOnPos(Vector3 pos, bool showStar, UIWidget goToWidget)
	{
		UITools.SetActive(labelContainer, true);
		UITools.SetActive(countingLabel, !showStar);
		UITools.SetActive(star, showStar);
		Vector3 startPos = UICamera.mainCamera.ScreenToWorldPoint(Camera.main.WorldToScreenPoint(pos));
		startPos = labelContainer.cachedTransform.parent.InverseTransformPoint(startPos);
		startPos.z = labelContainer.cachedTransform.position.z;
		Vector3 startScale = new Vector3(1f, 1f, 1f) * 2.5f;
		Vector3 endScale = Vector3.one;
		Vector3 endPos = startPos;
		if (goToWidget != null)
		{
			endPos = labelContainer.cachedTransform.parent.InverseTransformPoint(goToWidget.cachedTransform.position);
			endPos.z = startPos.z;
		}
		labelContainer.cachedTransform.localPosition = startPos;
		float inDuration = 0.001f;
		float delay = 0.01f;
		float fadeInAlpha = 0.75f;
		StartCoroutine(UIAnimTools.DoFadeFromTo(labelContainer, 0f, fadeInAlpha, inDuration));
		yield return StartCoroutine(UIAnimTools.DoScaleFromTo(labelContainer, startScale, endScale, inDuration));
		yield return new WaitForSeconds(delay);
		if (goToWidget != null)
		{
			StartCoroutine(UIAnimTools.DoMoveFromTo(labelContainer.cachedTransform, startPos, endPos, 0.4f, null));
			StartCoroutine(UIAnimTools.DoScaleFromTo(labelContainer, Vector3.one, Vector3.one * 2f, 0.4f));
			yield return StartCoroutine(UIAnimTools.DoFadeFromTo(labelContainer, fadeInAlpha, 0.2f, 0.45f));
		}
		else
		{
			yield return StartCoroutine(UIAnimTools.DoFadeFromTo(labelContainer, fadeInAlpha, 0f, 0.2f));
		}
		UITools.SetActive(labelContainer, false);
	}

	private IEnumerator DoAnimateScoreUpdate(UILabel label)
	{
		Vector3 startScale = Vector3.one * scoreScaleFrom;
		Vector3 endScale = Vector3.one;
		label.cachedTransform.localScale = startScale;
		float realTime = 0f;
		while (realTime < scoreScaleDuration)
		{
			realTime += RealTime.deltaTime;
			label.cachedTransform.localScale = Vector3.Lerp(startScale, endScale, realTime / scoreScaleDuration);
			yield return null;
		}
		label.cachedTransform.localScale = endScale;
	}

	public void Update()
	{
		fillSprite.fillAmount = Mathf.Lerp(fillSprite.fillAmount, desiredFillAmmount, RealTime.deltaTime * fillChangeSpeed);
		SetColorForFill();
	}
}
